<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li class="active"><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 扩展mongoose模型">1. 扩展mongoose模型</a></li><li><a href="#t12. statics 对类进行扩展">2. statics 对类进行扩展</a></li><li><a href="#t23. methods 对实例进行扩展">3. methods 对实例进行扩展</a></li><li><a href="#t34. virutal虚拟属性">4. virutal虚拟属性</a></li><li><a href="#t45. hook">5. hook</a></li><li><a href="#t56. schema 插件">6. schema 插件</a></li><li><a href="#t67.MongoDB 聚合">7.MongoDB 聚合</a><ul><li><a href="#t77.1 语法">7.1 语法</a></li><li><a href="#t87.2 分组">7.2 分组</a></li><li><a href="#t97.3 聚合的表达式">7.3 聚合的表达式</a></li><li><a href="#t107.4 管道的概念">7.4 管道的概念</a><ul><li><a href="#t117.4.1 过滤显示字段">7.4.1 过滤显示字段</a></li><li><a href="#t127.4.2 过滤文档">7.4.2 过滤文档</a></li><li><a href="#t137.4.3 跳过指定数量">7.4.3 跳过指定数量</a></li><li><a href="#t147.4.4 Mongoose中使用">7.4.4 Mongoose中使用</a></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x6269;&#x5C55;mongoose&#x6A21;&#x578B;">1. &#x6269;&#x5C55;mongoose&#x6A21;&#x578B; <a href="#t01. &#x6269;&#x5C55;mongoose&#x6A21;&#x578B;"> # </a></h2>
<p>&#x4E1A;&#x52A1;&#x5206;&#x5C42;</p>
<blockquote>
<p>service(&#x591A;&#x4E2A;&#x6A21;&#x578B;)-&gt;dao&#x5355;&#x4E2A;&#x6A21;&#x578B;-&gt;model &#x6A21;&#x578B;&#x5B9A;&#x4E49;</p>
<p>service(&#x591A;&#x4E2A;&#x6A21;&#x578B;)-&gt;dao&#x5355;&#x4E2A;&#x6A21;&#x578B;-&gt;model (&#x6A21;&#x578B;&#x5B9A;&#x4E49;+&#x6269;&#x5C55;&#x65B9;&#x6CD5;)</p>
</blockquote>
<h2 id="t12. statics &#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55;">2. statics &#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55; <a href="#t12. statics &#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55;"> # </a></h2>
<p>&#x6839;&#x636E;&#x7528;&#x6237;&#x540D;&#x67E5;&#x627E;&#x7528;&#x6237;&#x6587;&#x6863;</p>
<pre><code class="lang-js"> <span class="hljs-comment">//this&#x6307;&#x5411;model</span>
PersonSchema.statics.findByUsername = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">username, callback</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.findOne({ username }, callback);
}
Person.findByUsername(<span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, doc</span>) </span>{
    <span class="hljs-built_in">console</span>.log(doc);
});
</code></pre>
<h2 id="t23. methods &#x5BF9;&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x6269;&#x5C55;">3. methods &#x5BF9;&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x6269;&#x5C55; <a href="#t23. methods &#x5BF9;&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x6269;&#x5C55;"> # </a></h2>
<pre><code class="lang-js">PersonSchema.methods.exist = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
    <span class="hljs-keyword">let</span> query = { <span class="hljs-attr">username</span>: <span class="hljs-keyword">this</span>.username, <span class="hljs-attr">password</span>: <span class="hljs-keyword">this</span>.password };
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.model(<span class="hljs-string">&apos;Person&apos;</span>).findOne(query, callback);
}
<span class="hljs-keyword">let</span> person = <span class="hljs-keyword">new</span> Person({ <span class="hljs-attr">username</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">password</span>: <span class="hljs-string">&apos;123456&apos;</span>, <span class="hljs-attr">phone</span>: <span class="hljs-string">&apos;010-6255889&apos;</span>, <span class="hljs-attr">firstname</span>: <span class="hljs-string">&apos;first&apos;</span>, <span class="hljs-attr">lastname</span>: <span class="hljs-string">&apos;last&apos;</span> });
person.exist(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, doc</span>) </span>{
    <span class="hljs-built_in">console</span>.log(err, doc);
});
</code></pre>
<h2 id="t34. virutal&#x865A;&#x62DF;&#x5C5E;&#x6027;">4. virutal&#x865A;&#x62DF;&#x5C5E;&#x6027; <a href="#t34. virutal&#x865A;&#x62DF;&#x5C5E;&#x6027;"> # </a></h2>
<ul>
<li>virtual&#x662F;&#x865A;&#x62DF;&#x5C5E;&#x6027;&#x7684;&#x610F;&#x601D;&#xFF0C;&#x5373;&#x539F;&#x6765;Schema&#x5B9A;&#x4E49;&#x91CC;&#x662F;&#x4E0D;&#x5B58;&#x5728;&#x8BE5;&#x5C5E;&#x6027;&#xFF0C;&#x540E;&#x6765;&#x901A;&#x8FC7;virutal&#x65B9;&#x6CD5;&#x8D4B;&#x4E88;&#x7684;&#x5C5E;&#x6027;&#x3002;</li>
<li>Schema&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#x662F;&#x8981;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x91CC;&#x7684;&#xFF0C;&#x800C;virtual&#x5C5E;&#x6027;&#x57FA;&#x4E8E;&#x5DF2;&#x6709;&#x5C5E;&#x6027;&#x505A;&#x7684;&#x4E8C;&#x6B21;&#x5B9A;&#x4E49;&#x3002;<blockquote>
<p>&#x6A21;&#x578B;&#x5C5E;&#x6027; = Schema&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;+virtual&#x5C5E;&#x6027;</p>
</blockquote>
</li>
</ul>
<pre><code class="lang-js">PersonSchema.virtual(<span class="hljs-string">&apos;area&apos;</span>).get(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">//this&#x6307;&#x5411;&#x5B9E;&#x4F8B;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.phone.split(<span class="hljs-string">&apos;-&apos;</span>)[<span class="hljs-number">0</span>];
});
PersonSchema.virtual(<span class="hljs-string">&apos;number&apos;</span>).get(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.phone.split(<span class="hljs-string">&apos;-&apos;</span>)[<span class="hljs-number">1</span>];
});
<span class="hljs-keyword">let</span> Person = conn.model(<span class="hljs-string">&apos;Person&apos;</span>, PersonSchema);
<span class="hljs-keyword">let</span> person = <span class="hljs-keyword">new</span> Person({ <span class="hljs-attr">username</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">password</span>: <span class="hljs-string">&apos;123456&apos;</span>, <span class="hljs-attr">phone</span>: <span class="hljs-string">&apos;010-6255889&apos;</span>, <span class="hljs-attr">firstname</span>: <span class="hljs-string">&apos;first&apos;</span>, <span class="hljs-attr">lastname</span>: <span class="hljs-string">&apos;last&apos;</span> });
<span class="hljs-built_in">console</span>.log(person.fullname, person.area, person.number);
</code></pre>
<h2 id="t45. hook">5. hook <a href="#t45. hook"> # </a></h2>
<p>&#x5728;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x4FDD;&#x5B58;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x628A;&#x5BC6;&#x7801;&#x901A;&#x8FC7;salt&#x751F;&#x6210;hash&#x5BC6;&#x7801;&#xFF0C;&#x5E76;&#x518D;&#x6B21;&#x8D4B;&#x7ED9;password</p>
<pre><code class="lang-js">PersonSchema.pre(<span class="hljs-string">&apos;save&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">next</span>) </span>{
    <span class="hljs-keyword">this</span>.password = crypto.createHmac(<span class="hljs-string">&apos;sha256&apos;</span>, <span class="hljs-string">&apos;zfpx&apos;</span>).update(<span class="hljs-keyword">this</span>.password).digest(<span class="hljs-string">&apos;hex&apos;</span>);
    next();
});

PersonSchema.statics.login = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">username, password, callback</span>) </span>{
    password = crypto.createHmac(<span class="hljs-string">&apos;sha256&apos;</span>, <span class="hljs-string">&apos;zfpx&apos;</span>).update(password).digest(<span class="hljs-string">&apos;hex&apos;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.findOne({ username, password }, callback);
}

Person.login(<span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-string">&apos;123456&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, doc</span>) </span>{
    <span class="hljs-built_in">console</span>.log(err, doc);
});
</code></pre>
<h2 id="t56. schema &#x63D2;&#x4EF6;">6. schema &#x63D2;&#x4EF6; <a href="#t56. schema &#x63D2;&#x4EF6;"> # </a></h2>
<p>Schemas&#x662F;&#x53EF;&#x63D2;&#x62D4;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5B83;&#x4EEC;&#x63D0;&#x4F9B;&#x5728;&#x5E94;&#x7528;&#x9884;&#x5148;&#x6253;&#x5305;&#x80FD;&#x529B;&#x6765;&#x6269;&#x5C55;&#x4ED6;&#x4EEC;&#x7684;&#x529F;&#x80FD;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = exports = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">lastModified</span>(<span class="hljs-params">schema,options</span>)</span>{
  schema.add({<span class="hljs-attr">lastModify</span>:<span class="hljs-built_in">Date</span>});
  schema.pre(<span class="hljs-string">&apos;save&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
    <span class="hljs-keyword">this</span>.lastModify = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>;
    next();
  });
  <span class="hljs-keyword">if</span>(options&amp;&amp; options.index){
    schema.path(<span class="hljs-string">&apos;lastModify&apos;</span>).index(options.index);
  }
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> plugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./plugin&apos;</span>);
<span class="hljs-keyword">let</span> Person = <span class="hljs-keyword">new</span> Schema({});
Person.plugin(plugin,{<span class="hljs-attr">index</span>:<span class="hljs-literal">true</span>});
</code></pre>
<ul>
<li>Person &#x662F;&#x7528;&#x6237;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x7684;Schema</li>
<li>Person.plugin &#x662F;&#x4E3A;Person&#x589E;&#x52A0;plugin</li>
<li>plugin&#x6709;2&#x4E2A;&#x53C2;&#x6570;<ul>
<li>&#x63D2;&#x4EF6;&#x5BF9;&#x8C61; plugin </li>
<li>&#x914D;&#x7F6E;&#x9879; {index:true}</li>
</ul>
</li>
</ul>
<pre><code class="lang-js">schema.add({<span class="hljs-attr">age</span>:<span class="hljs-built_in">Number</span>});
</code></pre>
<h2 id="t67.MongoDB &#x805A;&#x5408;">7.MongoDB &#x805A;&#x5408; <a href="#t67.MongoDB &#x805A;&#x5408;"> # </a></h2>
<p>MongoDB&#x4E2D;&#x805A;&#x5408;(aggregate)&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x6570;&#x636E;(&#x8BF8;&#x5982;&#x7EDF;&#x8BA1;&#x5E73;&#x5747;&#x503C;,&#x6C42;&#x548C;&#x7B49;)&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x8BA1;&#x7B97;&#x540E;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x679C;&#x3002;&#x6709;&#x70B9;&#x7C7B;&#x4F3C;sql&#x8BED;&#x53E5;&#x4E2D;&#x7684; count(*)&#x3002;
aggregate() &#x65B9;&#x6CD5;
MongoDB&#x4E2D;&#x805A;&#x5408;&#x7684;&#x65B9;&#x6CD5;&#x4F7F;&#x7528;aggregate()&#x3002;</p>
<h3 id="t77.1 &#x8BED;&#x6CD5;">7.1 &#x8BED;&#x6CD5; <a href="#t77.1 &#x8BED;&#x6CD5;"> # </a></h3>
<p>aggregate() &#x65B9;&#x6CD5;&#x7684;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-js">&gt;db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
</code></pre>
<h3 id="t87.2 &#x5206;&#x7EC4;">7.2 &#x5206;&#x7EC4; <a href="#t87.2 &#x5206;&#x7EC4;"> # </a></h3>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4EE5;&#x4E0A;&#x96C6;&#x5408;&#x8BA1;&#x7B97;&#x6BCF;&#x4E2A;&#x4F5C;&#x8005;&#x6240;&#x5199;&#x7684;&#x6587;&#x7AE0;&#x6570;&#xFF0C;&#x4F7F;&#x7528;aggregate()&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js">&gt; db.article.insert({<span class="hljs-attr">uid</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">content</span>:<span class="hljs-string">&apos;1&apos;</span>});
&gt; db.article.insert({<span class="hljs-attr">uid</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">content</span>:<span class="hljs-string">&apos;2&apos;</span>});
&gt; db.article.insert({<span class="hljs-attr">uid</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">content</span>:<span class="hljs-string">&apos;3&apos;</span>});
</code></pre>
<pre><code class="lang-js"> db.article.aggregate([{<span class="hljs-attr">$group</span>:{<span class="hljs-attr">_id</span>:<span class="hljs-string">&apos;$uid&apos;</span>,<span class="hljs-attr">total</span>:{<span class="hljs-attr">$sum</span>:<span class="hljs-number">1</span>}}}]);
 { <span class="hljs-string">&quot;_id&quot;</span> : <span class="hljs-number">2</span>, <span class="hljs-string">&quot;total&quot;</span> : <span class="hljs-number">1</span> }
{ <span class="hljs-string">&quot;_id&quot;</span> : <span class="hljs-number">1</span>, <span class="hljs-string">&quot;total&quot;</span> : <span class="hljs-number">2</span> }
<span class="hljs-string">`
</span></code></pre>
<blockquote>
<p>select uid, count(*) total from article group by uid</p>
</blockquote>
<h3 id="t97.3 &#x805A;&#x5408;&#x7684;&#x8868;&#x8FBE;&#x5F0F;">7.3 &#x805A;&#x5408;&#x7684;&#x8868;&#x8FBE;&#x5F0F; <a href="#t97.3 &#x805A;&#x5408;&#x7684;&#x8868;&#x8FBE;&#x5F0F;"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x8868;&#x8FBE;&#x5F0F;</th>
<th style="text-align:left">&#x63CF;&#x8FF0;</th>
<th style="text-align:left">&#x5B9E;&#x4F8B;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">$sum</td>
<td style="text-align:left">&#x8BA1;&#x7B97;&#x603B;&#x548C;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, num_tutorial : {$sum : &quot;$visit&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$avg</td>
<td style="text-align:left">&#x8BA1;&#x7B97;&#x5E73;&#x5747;&#x503C;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, num_tutorial : {$avg : &quot;$visit&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$min</td>
<td style="text-align:left">&#x83B7;&#x53D6;&#x96C6;&#x5408;&#x4E2D;&#x6240;&#x6709;&#x6587;&#x6863;&#x5BF9;&#x5E94;&#x503C;&#x5F97;&#x6700;&#x5C0F;&#x503C;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, num_tutorial : {$min : &quot;$visit&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$max</td>
<td style="text-align:left">&#x83B7;&#x53D6;&#x96C6;&#x5408;&#x4E2D;&#x6240;&#x6709;&#x6587;&#x6863;&#x5BF9;&#x5E94;&#x503C;&#x5F97;&#x6700;&#x5927;&#x503C;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, num_tutorial : {$max : &quot;$visit&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$push</td>
<td style="text-align:left">&#x5728;&#x7ED3;&#x679C;&#x6587;&#x6863;&#x4E2D;&#x63D2;&#x5165;&#x503C;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4E2D;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, url : {$push: &quot;$url&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$addToSet</td>
<td style="text-align:left">&#x5728;&#x7ED3;&#x679C;&#x6587;&#x6863;&#x4E2D;&#x63D2;&#x5165;&#x503C;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4E2D;&#xFF0C;&#x4F46;&#x4E0D;&#x521B;&#x5EFA;&#x526F;&#x672C;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, url : {$addToSet : &quot;$url&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$first</td>
<td style="text-align:left">&#x6839;&#x636E;&#x8D44;&#x6E90;&#x6587;&#x6863;&#x7684;&#x6392;&#x5E8F;&#x83B7;&#x53D6;&#x7B2C;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x6570;&#x636E;&#x3002;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, first_url : {$first : &quot;$url&quot;}}}])</td>
</tr>
<tr>
<td style="text-align:left">$last</td>
<td style="text-align:left">&#x6839;&#x636E;&#x8D44;&#x6E90;&#x6587;&#x6863;&#x7684;&#x6392;&#x5E8F;&#x83B7;&#x53D6;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x6570;&#x636E;</td>
<td style="text-align:left">db.mycol.aggregate([{$group : {_id : &quot;$uid&quot;, last_url : {$last : &quot;$url&quot;}}}])</td>
</tr>
</tbody>
</table>
<pre><code class="lang-js">db.article.insert({<span class="hljs-attr">uid</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">content</span>:<span class="hljs-string">&apos;3&apos;</span>,<span class="hljs-attr">url</span>:<span class="hljs-string">&apos;url1&apos;</span>});
db.article.aggregate([{<span class="hljs-attr">$group</span> : {<span class="hljs-attr">_id</span> : <span class="hljs-string">&quot;$uid&quot;</span>, <span class="hljs-attr">url</span> : {<span class="hljs-attr">$push</span>: <span class="hljs-string">&quot;$url&quot;</span>}}}])
</code></pre>
<h3 id="t107.4 &#x7BA1;&#x9053;&#x7684;&#x6982;&#x5FF5;">7.4 &#x7BA1;&#x9053;&#x7684;&#x6982;&#x5FF5; <a href="#t107.4 &#x7BA1;&#x9053;&#x7684;&#x6982;&#x5FF5;"> # </a></h3>
<p>&#x7BA1;&#x9053;&#x5728;Unix&#x548C;Linux&#x4E2D;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5C06;&#x5F53;&#x524D;&#x547D;&#x4EE4;&#x7684;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4F5C;&#x4E3A;&#x4E0B;&#x4E00;&#x4E2A;&#x547D;&#x4EE4;&#x7684;&#x53C2;&#x6570;&#x3002;
MongoDB&#x7684;&#x805A;&#x5408;&#x7BA1;&#x9053;&#x5C06;MongoDB&#x6587;&#x6863;&#x5728;&#x4E00;&#x4E2A;&#x7BA1;&#x9053;&#x5904;&#x7406;&#x5B8C;&#x6BD5;&#x540E;&#x5C06;&#x7ED3;&#x679C;&#x4F20;&#x9012;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#x7BA1;&#x9053;&#x5904;&#x7406;&#x3002;&#x7BA1;&#x9053;&#x64CD;&#x4F5C;&#x662F;&#x53EF;&#x4EE5;&#x91CD;&#x590D;&#x7684;&#x3002;</p>
<ul>
<li>$project&#xFF1A;&#x4FEE;&#x6539;&#x8F93;&#x5165;&#x6587;&#x6863;&#x7684;&#x7ED3;&#x6784;&#x3002;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x91CD;&#x547D;&#x540D;&#x3001;&#x589E;&#x52A0;&#x6216;&#x5220;&#x9664;&#x57DF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x4EE5;&#x53CA;&#x5D4C;&#x5957;&#x6587;&#x6863;&#x3002;</li>
<li>$match&#xFF1A;&#x7528;&#x4E8E;&#x8FC7;&#x6EE4;&#x6570;&#x636E;&#xFF0C;&#x53EA;&#x8F93;&#x51FA;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6587;&#x6863;&#x3002;$match&#x4F7F;&#x7528;MongoDB&#x7684;&#x6807;&#x51C6;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;&#x3002;</li>
<li>$limit&#xFF1A;&#x7528;&#x6765;&#x9650;&#x5236;MongoDB&#x805A;&#x5408;&#x7BA1;&#x9053;&#x8FD4;&#x56DE;&#x7684;&#x6587;&#x6863;&#x6570;&#x3002;</li>
<li>$skip&#xFF1A;&#x5728;&#x805A;&#x5408;&#x7BA1;&#x9053;&#x4E2D;&#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x6587;&#x6863;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4F59;&#x4E0B;&#x7684;&#x6587;&#x6863;&#x3002;</li>
<li>$unwind&#xFF1A;&#x5C06;&#x6587;&#x6863;&#x4E2D;&#x7684;&#x67D0;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x7C7B;&#x578B;&#x5B57;&#x6BB5;&#x62C6;&#x5206;&#x6210;&#x591A;&#x6761;&#xFF0C;&#x6BCF;&#x6761;&#x5305;&#x542B;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x503C;&#x3002;</li>
<li>$group&#xFF1A;&#x5C06;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6587;&#x6863;&#x5206;&#x7EC4;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x7EDF;&#x8BA1;&#x7ED3;&#x679C;&#x3002;</li>
<li>$sort&#xFF1A;&#x5C06;&#x8F93;&#x5165;&#x6587;&#x6863;&#x6392;&#x5E8F;&#x540E;&#x8F93;&#x51FA;&#x3002;</li>
</ul>
<h4 id="t117.4.1 &#x8FC7;&#x6EE4;&#x663E;&#x793A;&#x5B57;&#x6BB5;">7.4.1 &#x8FC7;&#x6EE4;&#x663E;&#x793A;&#x5B57;&#x6BB5; <a href="#t117.4.1 &#x8FC7;&#x6EE4;&#x663E;&#x793A;&#x5B57;&#x6BB5;"> # </a></h4>
<pre><code class="lang-js">db.article.aggregate(
    { <span class="hljs-attr">$project</span> : {
        <span class="hljs-attr">_id</span>:<span class="hljs-number">0</span>,
        <span class="hljs-attr">content</span> : <span class="hljs-number">1</span> ,
    }}
 );
</code></pre>
<h4 id="t127.4.2 &#x8FC7;&#x6EE4;&#x6587;&#x6863;">7.4.2 &#x8FC7;&#x6EE4;&#x6587;&#x6863; <a href="#t127.4.2 &#x8FC7;&#x6EE4;&#x6587;&#x6863;"> # </a></h4>
<pre><code class="lang-js">db.article.aggregate( [
                        { <span class="hljs-attr">$match</span> : { <span class="hljs-attr">visit</span> : { <span class="hljs-attr">$gt</span> : <span class="hljs-number">10</span>, <span class="hljs-attr">$lte</span> : <span class="hljs-number">200</span> } } },
                        { <span class="hljs-attr">$group</span>: { <span class="hljs-attr">_id</span>: <span class="hljs-string">&apos;$uid&apos;</span>, <span class="hljs-attr">count</span>: { <span class="hljs-attr">$sum</span>: <span class="hljs-number">1</span> } } }
 ] );
</code></pre>
<h4 id="t137.4.3 &#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF;">7.4.3 &#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF; <a href="#t137.4.3 &#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF;"> # </a></h4>
<pre><code class="lang-js">db.article.aggregate( [
                        { <span class="hljs-attr">$match</span> : { <span class="hljs-attr">visit</span> : { <span class="hljs-attr">$gt</span> : <span class="hljs-number">10</span>, <span class="hljs-attr">$lte</span> : <span class="hljs-number">200</span> } } },
                        { <span class="hljs-attr">$group</span>: { <span class="hljs-attr">_id</span>: <span class="hljs-string">&apos;$uid&apos;</span>, <span class="hljs-attr">count</span>: { <span class="hljs-attr">$sum</span>: <span class="hljs-number">1</span> } } },
                        { <span class="hljs-attr">$skip</span> : <span class="hljs-number">1</span> }
 ] );
</code></pre>
<h4 id="t147.4.4 Mongoose&#x4E2D;&#x4F7F;&#x7528;">7.4.4 Mongoose&#x4E2D;&#x4F7F;&#x7528; <a href="#t147.4.4 Mongoose&#x4E2D;&#x4F7F;&#x7528;"> # </a></h4>
<pre><code class="lang-js">Article.aggregate([
                        { <span class="hljs-attr">$match</span> : { <span class="hljs-attr">visit</span> : { <span class="hljs-attr">$gt</span> : <span class="hljs-number">10</span>, <span class="hljs-attr">$lte</span> : <span class="hljs-number">200</span> } } },
                        { <span class="hljs-attr">$group</span>: { <span class="hljs-attr">_id</span>: <span class="hljs-string">&apos;$uid&apos;</span>, <span class="hljs-attr">count</span>: { <span class="hljs-attr">$sum</span>: <span class="hljs-number">1</span> } } },
                        { <span class="hljs-attr">$skip</span> : <span class="hljs-number">1</span> }
 ])
                .
</code></pre>
<ul>
<li><a href="https://github.com/sodino/MongoDemo/blob/master/app.js">MongoDemo</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
